package cn.edu.cimv.service.impl;

import cn.edu.cimv.dao.entity.Member;
import cn.edu.cimv.dao.entity.MemberItem;
import cn.edu.cimv.dao.entity.User;
import cn.edu.cimv.dao.entity.UserMemberRel;
import cn.edu.cimv.dao.mapper.UserMapper;
import cn.edu.cimv.service.UserService;
import cn.edu.cimv.util.CryptionUtil;
import cn.edu.cimv.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by 门铎 on 2016/7/26.
 */
@Service
public class UserServiceImpl implements UserService{
    private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> queryUsers() {
        logger.info("查询所有用户信息");
        List<User> userList =userMapper.queryUsers();
        List<UserMemberRel> userMemberRelList = userMapper.selectUserMemberRel();
        for (User u:userList) {
            for(UserMemberRel m:userMemberRelList){
                if (u.getId() ==m.getId()){
                    u.setSid(m.getSid());
                    break;
                }
            }
        }
        return userList;

    }

    @Override
    public User queryUserById(int id) {
        User user = userMapper.queryUserById(id);
        try {
            user.setPassword(CryptionUtil.decryptByPrivateKey(user.getPassword()));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("密码解密出现异常！");
        }

        return user;
    }

    @Override
    public int insertUser(String userName, String password, String role) {

        if(userMapper.selectCountByUserName(userName,role)==0){
            //新用户
            User user = new User();
            user.setUserName(userName);
            user.setHomeUrl(userName);
            //密码加密
            try {
                user.setPassword(CryptionUtil.encryptByPublicKey(password));
            } catch (Exception e) {
                e.printStackTrace();
            }
            user.setRole(role);
            user.setCreateDate(DateUtil.toDateString(new Date()));
            user.setUpdateDate(DateUtil.toDateString(new Date()));
            userMapper.insertUser(user);
            return 0;
        }else if(role.equals("1")){
            return 1;
        }else{
            return 2;
        }

    }

    @Override
    public int updateUser(String userName,String password,String role,int id) {

        if(userMapper.selectCountByUserName(userName,role)==0){
            //新用户
            User user = new User();
            user.setId(id);
            user.setUserName(userName);
            user.setHomeUrl(userName);
            //密码加密
            try {
                user.setPassword(CryptionUtil.encryptByPublicKey(password));
            } catch (Exception e) {
                e.printStackTrace();
            }
            user.setRole(role);
            user.setUpdateDate(DateUtil.toDateString(new Date()));
            userMapper.updateUser(user);
            return 0;
        }else {
            return 1;
        }

    }

    @Override
    @Transactional
    public void deleteUser(int id) {
        userMapper.deleteUser(id);
        userMapper.deleteMemberByRefId(id);
    }

    @Override
    public Member selectMember(int refId) {
        if(userMapper.selectMemberByRefId(refId)==0){
            return null;
        }else{
            return userMapper.selectMember(refId);
        }
    }


    @Override
    public int selectMemberId(String userName) {
        return userMapper.selectMemberId(userName);
    }

    /**
     * 更新实验室人员信息
     * @param member 实验室人员信息
     */
    @Override
    public void updateMember(Member member) {
        member.setUpdateDate(DateUtil.toDateString(new Date()));
        //如果表中无此数据 插入 否则更新
        if(userMapper.selectMemberByRefId(member.getRefId())==0){
            userMapper.insertMember(member);
        }else{
            userMapper.updateMember(member);
        }
        logger.info("更新成员信息成功！");
    }

    @Override
    public List<MemberItem> selectMemberItems(String userId) {
        return userMapper.getMemberItemsById(userId);
    }

    @Override
    public void insertMemberItem(MemberItem memberItem) {
        userMapper.insertMemberItem(memberItem);
    }

    @Override
    public void updateMemberItem(MemberItem memberItem) {
        userMapper.updateMemberItem(memberItem);
    }

    @Override
    public MemberItem getMemberItemsByKey(int id) {
        return userMapper.getMemberItemsByKey(id);
    }

    @Override
    public void deleteMemberItem(int id) {
        userMapper.deleteMemberItem(id);
    }
}
